package com.xhtt.modules.app.controller.task;

import com.alibaba.fastjson.JSON;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.sys.controller.AbstractController;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.task.entity.TaskServicePlan;
import com.xhtt.modules.task.entity.TaskServiceTask;
import com.xhtt.modules.task.model.TaskOperModel;
import com.xhtt.modules.task.model.service.ArrangeModel;
import com.xhtt.modules.task.model.service.TaskServiceOtherModel;
import com.xhtt.modules.task.service.TaskServicePlanService;
import com.xhtt.modules.task.service.TaskServiceTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 任务安排-维修任务
 * </p>
 *
 * @author feipc
 * @since 2019-03-11
 */
@Api(tags = "任务安排-维修任务", value = "任务安排-维修任务")
@RestController
@RequestMapping("app/task/service")
public class TaskServicePlanController extends AbstractController {

    @Autowired
    private TaskServicePlanService taskServicePlanService;
    @Autowired
    private TaskServiceTaskService taskServiceTaskService;

    /**
     * 获取操作计划列表
     */
    @GetMapping(value = "/planList")
    @ApiOperation("获取操作计划列表(web)")
    @Login
    public R planList(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        PageUtils page = taskServicePlanService.planList(params, user);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增操作计划
     */
    @PostMapping(value = "/addPlan")
    @ApiOperation("新增操作计划(web)")
    @Login
    public R addPlan(@RequestBody TaskServicePlan taskServicePlan) {
        taskServicePlanService.addPlan(taskServicePlan);
        return R.ok();
    }

    /**
     * 删除操作计划
     */
    @PostMapping(value = "/deletePlan")
    @ApiOperation("删除操作计划(web)")
    @Login
    public R deletePlan(@RequestBody Integer[] ids) {
        taskServicePlanService.deletePlan(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 修改操作计划
     */
    @PostMapping(value = "/updatePlan")
    @ApiOperation("修改操作计划(web)")
    @Login
    public R updatePlan(@RequestBody TaskServicePlan taskServicePlan) {
        taskServicePlanService.updatePlan(taskServicePlan);
        return R.ok();
    }

    /**
     * 获取操作计划详情
     */
    @GetMapping(value = "/getPlan/{id}")
    @ApiOperation("获取操作计划详情")
    @Login
    public R getPlan(@PathVariable("id") Integer id) {
        TaskServicePlan taskServicePlan = taskServicePlanService.getPlan(id);
        return R.ok().put(Constant.DATA, taskServicePlan);
    }

    /**
     * 提交操作计划
     */
    @PostMapping(value = "/commitPlan")
    @ApiOperation("提交操作计划(web)")
    @Login
    public R commitPlan(@RequestParam Integer id) {
        taskServicePlanService.commitPlan(id);
        return R.ok();
    }

    /**
     * 任务安排弹出页面的展示内容
     */
    @GetMapping(value = "/getArrangeInfo")
    @ApiOperation("获取-任务安排弹出页面的展示内容(web)")
    @Login
    public R getArrangeInfo(@RequestParam Integer id) {
        ArrangeModel arrangeModel = taskServicePlanService.getArrangeInfo(id);
        return R.ok().put(Constant.DATA, arrangeModel);
    }

    /**
     * 任务安排提交
     */
    @PostMapping(value = "/arrange")
    @ApiOperation("任务安排提交（web）")
    @Login
    public R arrange(@RequestBody ArrangeModel arrangeModel) {
        if (null == arrangeModel.getPlanId()) {
            throw new RRException("参数有误");
        }
        taskServicePlanService.arrange(arrangeModel);
        return R.ok();
    }

    /**
     * 任务分配-退回
     */
    @PostMapping(value = "/back")
    @ApiOperation("任务分配-退回（web）")
    @Login
    public R back(@RequestBody TaskServicePlan plan) {
        taskServicePlanService.back(plan);
        return R.ok();
    }

    /**
     * 指派任务页面-查看任务
     */
    @GetMapping(value = "/getTaskDetail")
    @ApiOperation("指派任务页面-查看任务(web)")
    @Login
    public R getTaskDetail(@RequestParam Integer id, @ApiIgnore @LoginUser SysUserEntity user) {
        ArrangeModel am = taskServiceTaskService.getTaskDetail(id, user);
        return R.ok().put(Constant.DATA, am);
    }

    /**
     * 指派任务
     */
    @PostMapping(value = "/pubTask")
    @ApiOperation("指派任务(web)")
    @Login
    public R pubTask(@RequestBody List<TaskServiceTask> tasks, @ApiIgnore @LoginUser SysUserEntity user) {
        //其实是更新字段操作，所以判断一下id、
        if (null == tasks.get(0).getId()) {
            throw new RRException("参数有误");
        }
        taskServiceTaskService.pubTask(tasks, user);
        return R.ok();
    }

    /**
     * 新增临时任务
     */
    @PostMapping(value = "/pubTempTask")
    @ApiOperation("新增临时任务(web)")
    @Login
    public R pubTempTask(@RequestBody TaskServicePlan servicePlan, @ApiIgnore @LoginUser SysUserEntity user) {
        if (null == servicePlan.getTaskServiceTask()) {
            throw new RRException("参数有误");
        }
        MDC.put("user", JSON.toJSONString(user));
        taskServiceTaskService.pubTempTask(servicePlan);
        return R.ok();
    }


    /**
     * 任务确认-任务列表
     */
    @GetMapping(value = "/confirmTaskList")
    @ApiOperation("任务确认-任务列表(web)")
    @Login
    public R confirmTaskList(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        params.put("confirmUserId", user.getUserId());
        PageUtils page = taskServiceTaskService.confirmTaskList(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 任务确认-查看任务作业详情
     */
    @GetMapping(value = "/taskServiceDetail")
    @ApiOperation("任务确认-查看任务作业详情(web/app)")
    @Login
    public R taskServiceDetail(@RequestParam Integer taskId) {
        TaskOperModel model = taskServiceTaskService.taskServiceDetail(taskId);
        return R.ok().put(Constant.DATA, model);
    }

    /**
     * APP点击开始操作
     */
    @PostMapping(value = "/start")
    @ApiOperation("APP点击开始操作(app)")
    @Login
    public R start(Integer taskId) {
        taskServiceTaskService.start(taskId);
        return R.ok();
    }


    /**
     * APP临时保存任务
     */
    @PostMapping(value = "/tempSave")
    @ApiOperation("APP临时保存任务(app)")
    @Login
    public R tempSave(@RequestBody TaskServiceOtherModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskServiceTaskService.tempSave(model);
        return R.ok();
    }

    /**
     * APP提交任务
     */
    @PostMapping(value = "/submitTask")
    @ApiOperation("APP提交任务(app)")
    @Login
    public R submitTask(@RequestBody TaskServiceOtherModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskServiceTaskService.submitTask(model);
        return R.ok();
    }

    /**
     * 任务确认-任务确认的提交
     */
    @PostMapping(value = "/commitTask")
    @ApiOperation("任务确认的提交(web/app)")
    @Login
    public R commitTask(@RequestBody TaskOperModel model) {
        taskServiceTaskService.commitTask(model);
        return R.ok();
    }

}
